package org.example.visitor;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.example.autosuggest.AutoSuggester;
import org.example.autosuggest.LexerAndParserFactory;
import org.example.autosuggest.ReflectionLexerAndParserFactory;

import java.util.Collection;

public class SuggestWithMysql {
    public static final String CREATE_TABLE_DDL = "CREATE TABLE wcp_white_list_add_job_arch(\n" +
            "    id VARCHAR(32) NOT NULL DEFAULT '' COMMENT 'xx',\n" +
            "    ptn_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'aa',\n" +
            "    biz_seq_no VARCHAR(32) NOT NULL DEFAULT '' COMMENT 'bb',\n" +
            "    id_no VARCHAR(60) NOT NULL DEFAULT '' COMMENT 'cc',\n" +
            "    cust_name VARCHAR(70) NOT NULL DEFAULT '' COMMENT 'dd',\n" +
            "    phone VARCHAR(30) NOT NULL DEFAULT '' COMMENT 'ee',\n" +
            "    next_fire_time BIGINT(15) COMMENT 'nn',\n" +
            "    job_status VARCHAR(2) NOT NULL DEFAULT '' COMMENT 'ss',\n" +
            "    retry_count TINYINT(4) NOT NULL COMMENT 'hh',\n" +
            "    result_code VARCHAR(20) NOT NULL DEFAULT '' COMMENT 'qq',\n" +
            "    result_msg VARCHAR(255) NOT NULL DEFAULT '' COMMENT 'wew',\n" +
            "    created_by VARCHAR(20) NOT NULL DEFAULT '' COMMENT 'tt',\n" +
            "    created_date TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'wq',\n" +
            "    last_modified_by VARCHAR(20) NOT NULL DEFAULT '' COMMENT 'hweh',\n" +
            "    last_modified_date TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'asdf',\n" +
            "    PRIMARY KEY (id, ptn_date),\n" +
            "    KEY idx_biz_seq_no(biz_seq_no),\n" +
            "    KEY idx_pending(job_status, retry_count, next_fire_time)\n" +
            ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='aewrh'\n" +
            "PARTITION BY RANGE COLUMNS (ptn_date) (\n" +
            "PARTITION p20221227 VALUES LESS THAN ('2022-12-27') ENGINE = InnoDB,\n" +
            "PARTITION p20230103 VALUES LESS THAN ('2023-01-03') ENGINE = InnoDB,\n" +
            "PARTITION p20230110 VALUES LESS THAN ('2023-01-10') ENGINE = InnoDB,\n" +
            "PARTITION p20230117 VALUES LESS THAN ('2023-01-17') ENGINE = InnoDB,\n" +
            "PARTITION p20230124 VALUES LESS THAN ('2023-01-24') ENGINE = InnoDB,\n" +
            "PARTITION p20230131 VALUES LESS THAN ('2023-01-31') ENGINE = InnoDB,\n" +
            "PARTITION p20230207 VALUES LESS THAN ('2023-02-07') ENGINE = InnoDB,\n" +
            "PARTITION p20230214 VALUES LESS THAN ('2023-02-14') ENGINE = InnoDB,\n" +
            "PARTITION p20230221 VALUES LESS THAN ('2023-02-21') ENGINE = InnoDB,\n" +
            "PARTITION p20230228 VALUES LESS THAN ('2023-02-28') ENGINE = InnoDB,\n" +
            "PARTITION p20230307 VALUES LESS THAN ('2023-03-07') ENGINE = InnoDB,\n" +
            "PARTITION p20230314 VALUES LESS THAN ('2023-03-14') ENGINE = InnoDB,\n" +
            "PARTITION p20230321 VALUES LESS THAN ('2023-03-21') ENGINE = InnoDB,\n" +
            "PARTITION p20230328 VALUES LESS THAN ('2023-03-28') ENGINE = InnoDB,\n" +
            "PARTITION p20230404 VALUES LESS THAN ('2023-04-04') ENGINE = InnoDB,\n" +
            "PARTITION p20230411 VALUES LESS THAN ('2023-04-11') ENGINE = InnoDB,\n" +
            "PARTITION p20230418 VALUES LESS THAN ('2023-04-18') ENGINE = InnoDB,\n" +
            "PARTITION p20230425 VALUES LESS THAN ('2023-04-25') ENGINE = InnoDB,\n" +
            "PARTITION p20230502 VALUES LESS THAN ('2023-05-02') ENGINE = InnoDB,\n" +
            "PARTITION p20230509 VALUES LESS THAN ('2023-05-09') ENGINE = InnoDB\n" +
            ");";

    public static void main(String[] args) {
//        LexerAndParserFactory lexerAndParserfactory = new ReflectionLexerAndParserFactory(
//                com.neuronbit.parser.MySqlLexer.class, com.neuronbit.parser.MySqlParser.class);
//        Collection<String> suggestions = new AutoSuggester(lexerAndParserfactory, CRATE_TABLE_DDL).suggestCompletions();
//
//        if (null != suggestions && !suggestions.isEmpty()) {
//            for (String suggestion : suggestions) {
//                System.out.println(suggestion);
//            }
//
//        } else {
//            System.out.println("suggestion is empty");
//        }

        CharStream input = CharStreams.fromString(CREATE_TABLE_DDL);
        com.neuronbit.parser.MySqlLexer lexer = new com.neuronbit.parser.MySqlLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        com.neuronbit.parser.MySqlParser parser = new com.neuronbit.parser.MySqlParser(tokens);
        ParseTree root = parser.root();
        IdentifierVisitor identifierVisitor = new IdentifierVisitor();
        identifierVisitor.visit(root);

        System.out.println(identifierVisitor.getTableInfo());
    }
}
